package com.hamizhong.iflowers.web.action.admin.prodgoods;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import com.hamizhong.iflowers.service.IFestivalService;
import com.hamizhong.iflowers.service.IFlowerCountService;
import com.hamizhong.iflowers.service.IFlowerFloridService;
import com.hamizhong.iflowers.service.IGoodsFestivalMapService;
import com.hamizhong.iflowers.service.IGoodsProductMapService;
import com.hamizhong.iflowers.service.IGoodsPurposeMapService;
import com.hamizhong.iflowers.service.IGoodsService;
import com.hamizhong.iflowers.service.IGoodsTargetMapService;
import com.hamizhong.iflowers.service.IProductService;
import com.hamizhong.iflowers.service.IPurposeService;
import com.hamizhong.iflowers.service.ITargetService;
import com.hamizhong.iflowers.utils.MyColumnIndicator;
import com.hamizhong.iflowers.utils.MyTools;
import com.hamizhong.iflowers.web.base.MyBaseAction;
import com.hamizhong.iflowers.web.dto.IOPResultDto;
import com.hamizhong.iflowers.web.vo.FestivalVO;
import com.hamizhong.iflowers.web.vo.FlowerCountVO;
import com.hamizhong.iflowers.web.vo.FlowerFloridVO;
import com.hamizhong.iflowers.web.vo.GoodsFestivalMapVO;
import com.hamizhong.iflowers.web.vo.GoodsProductMapVO;
import com.hamizhong.iflowers.web.vo.GoodsPurposeMapVO;
import com.hamizhong.iflowers.web.vo.GoodsTargetMapVO;
import com.hamizhong.iflowers.web.vo.GoodsVO;
import com.hamizhong.iflowers.web.vo.IObjectVO;
import com.hamizhong.iflowers.web.vo.ProductVO;
import com.hamizhong.iflowers.web.vo.PurposeVO;
import com.hamizhong.iflowers.web.vo.TargetVO;

public class DoGoodsAddAction extends MyBaseAction {

	private IGoodsService iGoodsService = null;
	private IProductService iProductService = null;
	private IPurposeService iPurposeService = null;
	private IFestivalService iFestivalService = null;
	private ITargetService iTargetService = null;
	private IFlowerFloridService iFlowerFloridService = null;
	private IFlowerCountService iFlowerCountService = null;
	private IGoodsProductMapService iGoodsProductMapService = null;
	private IGoodsPurposeMapService iGoodsPurposeMapService = null;
	private IGoodsFestivalMapService iGoodsFestivalMapService = null;
	private IGoodsTargetMapService iGoodsTargetMapService = null;
	
	public IGoodsService getiGoodsService() {
		return iGoodsService;
	}
	public void setiGoodsService(IGoodsService iGoodsService) {
		this.iGoodsService = iGoodsService;
	}
	public IProductService getiProductService() {
		return iProductService;
	}
	public void setiProductService(IProductService iProductService) {
		this.iProductService = iProductService;
	}
	public IPurposeService getiPurposeService() {
		return iPurposeService;
	}
	public void setiPurposeService(IPurposeService iPurposeService) {
		this.iPurposeService = iPurposeService;
	}
	public IFestivalService getiFestivalService() {
		return iFestivalService;
	}
	public void setiFestivalService(IFestivalService iFestivalService) {
		this.iFestivalService = iFestivalService;
	}
	public ITargetService getiTargetService() {
		return iTargetService;
	}
	public void setiTargetService(ITargetService iTargetService) {
		this.iTargetService = iTargetService;
	}
	public IFlowerFloridService getiFlowerFloridService() {
		return iFlowerFloridService;
	}
	public void setiFlowerFloridService(IFlowerFloridService iFlowerFloridService) {
		this.iFlowerFloridService = iFlowerFloridService;
	}
	public IFlowerCountService getiFlowerCountService() {
		return iFlowerCountService;
	}
	public void setiFlowerCountService(IFlowerCountService iFlowerCountService) {
		this.iFlowerCountService = iFlowerCountService;
	}
	public IGoodsProductMapService getiGoodsProductMapService() {
		return iGoodsProductMapService;
	}
	public void setiGoodsProductMapService(
			IGoodsProductMapService iGoodsProductMapService) {
		this.iGoodsProductMapService = iGoodsProductMapService;
	}
	public IGoodsPurposeMapService getiGoodsPurposeMapService() {
		return iGoodsPurposeMapService;
	}
	public void setiGoodsPurposeMapService(
			IGoodsPurposeMapService iGoodsPurposeMapService) {
		this.iGoodsPurposeMapService = iGoodsPurposeMapService;
	}
	public IGoodsFestivalMapService getiGoodsFestivalMapService() {
		return iGoodsFestivalMapService;
	}
	public void setiGoodsFestivalMapService(
			IGoodsFestivalMapService iGoodsFestivalMapService) {
		this.iGoodsFestivalMapService = iGoodsFestivalMapService;
	}
	public IGoodsTargetMapService getiGoodsTargetMapService() {
		return iGoodsTargetMapService;
	}
	public void setiGoodsTargetMapService(
			IGoodsTargetMapService iGoodsTargetMapService) {
		this.iGoodsTargetMapService = iGoodsTargetMapService;
	}

	// 接受依赖注入的属性
	private String savePath;

	// 接受依赖注入的方法
	public void setSavePath(String value) {
		this.savePath = value;
	}
	private String getSavePath() throws Exception {
		//return this.getRQ().getRealPath("/");
		return this.getRQ().getSession().getServletContext().getRealPath("/");
	}
	
	private File[] upload;// 实际上传文件
	private String[] uploadContentType;// 文件的内容类型
	private String[] uploadFileName;// 上传文件名
	
	public File[] getUpload() {
		return upload;
	}
	public void setUpload(File[] upload) {
		this.upload = upload;
	}
	public String[] getUploadContentType() {
		return uploadContentType;
	}
	public void setUploadContentType(String[] uploadContentType) {
		this.uploadContentType = uploadContentType;
	}
	public String[] getUploadFileName() {
		return uploadFileName;
	}
	public void setUploadFileName(String[] uploadFileName) {
		this.uploadFileName = uploadFileName;
	}
	
	@Override
	protected String doRequest() {
		logger.info("----------------- Hello DoGoodsAddAction -----------------");
		String goodsName = getRQ().getParameter("goodsName");
		String goodsPrice = getRQ().getParameter("goodsPrice");
		String[] purposeIds = getRQ().getParameterValues("purposeId");
		String[] festivalIds = getRQ().getParameterValues("festivalId");
		String[] targetIds = getRQ().getParameterValues("targetId");
		String flowerFloridId = getRQ().getParameter("flowerFloridId");
		String flowerCountId = getRQ().getParameter("flowerCountId");
		String sortId = getRQ().getParameter("sortId");
		String remarks = getRQ().getParameter("remarks");
		
		String[] productIds = getRQ().getParameterValues("productIds");
		String goodsDescribe = getRQ().getParameter("goodsDescribe");
		
		if(productIds.length >= 1) {
			for(int i=0;i<productIds.length;i++) {
				logger.info("---------productIds:"+productIds[i]);
			}
		}
		logger.info("---------goodsName:"+goodsName);
		if(purposeIds.length >= 1) {
			for(int i=0;i<purposeIds.length;i++) {
				logger.info("---------purposeId:"+purposeIds[i]);
			}
		}
		if(festivalIds.length >= 1) {
			for(int i=0;i<festivalIds.length;i++) {
				logger.info("---------festivalId:"+festivalIds[i]);
			}
		}
		if(targetIds.length >= 1) {
			for(int i=0;i<targetIds.length;i++) {
				logger.info("---------targetId:"+targetIds[i]);
			}
		}
		logger.info("---------flowerFloridId:"+flowerFloridId);
		logger.info("---------flowerCountId:"+flowerCountId);
		logger.info("---------sortId:"+sortId);
		logger.info("---------remarks:"+remarks);
		
		logger.info("---------goodsDescribe:"+goodsDescribe);

		try {
			FileOutputStream fos = null;
			FileInputStream fis = null;
			
			File[] files = getUpload();
			String[] filesName = getUploadFileName();
			String[] filesType = getUploadContentType();
			
			String targetSavePath = "";
			String picturePath1 = "";
			String picturePath2 = "";
			String picturePath3 = "";
			String targetFileName1 = "";
			String targetFileName2 = "";
			String targetFileName3 = "";
			//循环存贮上传的图片
			for(int i=0;i<files.length;i++) {
				//i == 0；上传商品列表展示图
				if(i == 0) {
					System.out.println("SavePath------------------"+getSavePath()+ getUploadFileName()[i]);
					//String targetFileName = UUID.randomUUID().toString() + filesName[i];
					targetFileName1 = UUID.randomUUID().toString()+filesName[i].substring(filesName[i].lastIndexOf("."));
					targetSavePath = getSavePath() + "/upload/goodsPicture";
					File targetSavefile = new File(targetSavePath);
					if(!targetSavefile.exists()) {
						targetSavefile.mkdirs();
					}
					//设置存贮数据库路径
					picturePath1 = "upload/goodsPicture/"+targetFileName1;
					//创建输出流
					fos = new FileOutputStream(getSavePath() + picturePath1);
				}
				//i == 1；上传大图片
				if(i == 1) {
					System.out.println("SavePath------------------"+getSavePath()+ getUploadFileName()[i]);
					//String targetFileName = UUID.randomUUID().toString() + filesName[i];
					targetFileName2 = UUID.randomUUID().toString()+filesName[i].substring(filesName[i].lastIndexOf("."));;
					targetSavePath = getSavePath() + "/upload/goodsPicture";
					File targetSavefile = new File(targetSavePath);
					if(!targetSavefile.exists()) {
						targetSavefile.mkdirs();
					}
					//设置存贮数据库路径
					picturePath2 = "upload/goodsPicture/"+targetFileName2;
					//创建输出流
					fos = new FileOutputStream(getSavePath() + picturePath2);
				}
				//i == 2；上传特写图片
				if(i == 2) {
					System.out.println("SavePath------------------"+getSavePath()+ getUploadFileName()[i]);
					//String targetFileName = UUID.randomUUID().toString() + filesName[i];
					targetFileName3 = UUID.randomUUID().toString()+filesName[i].substring(filesName[i].lastIndexOf("."));;
					targetSavePath = getSavePath() + "/upload/goodsPicture";
					File targetSavefile = new File(targetSavePath);
					if(!targetSavefile.exists()) {
						targetSavefile.mkdirs();
					}
					//设置存贮数据库路径
					picturePath3 = "upload/goodsPicture/"+targetFileName3;
					//创建输出流
					fos = new FileOutputStream(getSavePath() + picturePath3);
				}
				//创建输入流
				fis = new FileInputStream(files[i]);
				byte[] buffer = new byte[4];
				int len = 0;
				while ((len = fis.read(buffer)) > 0) {
					fos.write(buffer, 0, len);
				}
			}
			fos.close();// 注意：流应当关闭。
			fis.close();
			
			//继续操作数据库存储
			GoodsVO goodsVO = new GoodsVO();
			FlowerFloridVO flowerFloridVO = new FlowerFloridVO();
			flowerFloridVO.setFlowerFloridId(Integer.parseInt(flowerFloridId));
			FlowerCountVO flowerCountVO = new FlowerCountVO();
			flowerCountVO.setFlowerCountId(Integer.parseInt(flowerCountId));
			
			goodsVO.setGoodsName(goodsName);
			goodsVO.setFlowerFloridVO(flowerFloridVO);
			goodsVO.setFlowerCountVO(flowerCountVO);
			goodsVO.setGoodsPrice(Float.parseFloat(goodsPrice));
			//默认新增商品不打折促销
			goodsVO.setIsBargin(MyColumnIndicator.G_BARGIN_ST_A0);
			goodsVO.setBarginPrice(new Float(-1));
			goodsVO.setGoodsDescribe(goodsDescribe);
			//默认新增商品为有效状态
			goodsVO.setState(MyColumnIndicator.G_ST_A);
			goodsVO.setSortId(Integer.parseInt(sortId));
			goodsVO.setRemarks(remarks);

			//存入图片
			goodsVO.setGoodsPictureS(picturePath1);
			goodsVO.setGoodsPictureM(picturePath2);
			goodsVO.setGoodsPictureL(picturePath3);
			
			//先存入商品实例
			IOPResultDto<IObjectVO> resultDto = new IOPResultDto<IObjectVO>();
			resultDto = iGoodsService.addGoods(goodsVO);
			int insGoodsId = 0;
			if(resultDto.isSuccess()) {
				//获取已经存入数据库中的商品实例ID
				insGoodsId = resultDto.getFirstResult().getGoodsVOList().get(0).getGoodsId();
				GoodsVO goodsVOTrans = new GoodsVO();
				goodsVOTrans.setGoodsId(insGoodsId);

				//开始存映射关联表
				//商品产品关联
				if(productIds.length >= 1) {
					ProductVO productVO = null;
					GoodsProductMapVO goodsProductMapVO = null;
					List<GoodsProductMapVO> goodsProductMapVOList = new ArrayList<GoodsProductMapVO>();
					for(int i=0;i<productIds.length;i++) {
						productVO = new ProductVO();
						productVO.setProductId(Integer.parseInt(productIds[i]));
						goodsProductMapVO = new GoodsProductMapVO();
						goodsProductMapVO.setGoodsVO(goodsVOTrans);
						goodsProductMapVO.setMapId(0);
						goodsProductMapVO.setProductVO(productVO);
						goodsProductMapVOList.add(goodsProductMapVO);
					}
					if(goodsProductMapVOList.size() > 0) {
						IOPResultDto<IObjectVO> dtoGoodsProductMap = iGoodsProductMapService.addGoodsProductMaps(goodsProductMapVOList);
						if(!dtoGoodsProductMap.isSuccess()) {
							return ERROR;
						}
					}
				}
				//商品用途关联
				if(purposeIds.length >= 1) {
					PurposeVO purposeVO = null;
					GoodsPurposeMapVO goodsPurposeMapVO = null;
					List<GoodsPurposeMapVO> goodsPurposeMapVOList = new ArrayList<GoodsPurposeMapVO>();
					for(int i=0;i<purposeIds.length;i++) {
						purposeVO = new PurposeVO();
						purposeVO.setPurposeId(purposeIds[i]);
						goodsPurposeMapVO = new GoodsPurposeMapVO();
						goodsPurposeMapVO.setGoodsVO(goodsVOTrans);
						goodsPurposeMapVO.setMapId(0);
						goodsPurposeMapVO.setPurposeVO(purposeVO);
						goodsPurposeMapVOList.add(goodsPurposeMapVO);
					}
					if(goodsPurposeMapVOList.size() > 0) {
						IOPResultDto<IObjectVO> dtoGoodsPurposeMap = iGoodsPurposeMapService.addGoodsPurposeMaps(goodsPurposeMapVOList);
						if(!dtoGoodsPurposeMap.isSuccess()) {
							return ERROR;
						}
					}
				}
				//商品节日关联
				if(festivalIds.length >= 1) {
					FestivalVO festivalVO = null;
					GoodsFestivalMapVO goodsFestivalMapVO = null;
					List<GoodsFestivalMapVO> goodsFestivalMapVOList = new ArrayList<GoodsFestivalMapVO>();
					for(int i=0;i<festivalIds.length;i++) {
						festivalVO = new FestivalVO();
						festivalVO.setFestivalId(festivalIds[i]);
						goodsFestivalMapVO = new GoodsFestivalMapVO();
						goodsFestivalMapVO.setGoodsVO(goodsVOTrans);
						goodsFestivalMapVO.setMapId(0);
						goodsFestivalMapVO.setFestivalVO(festivalVO);
						goodsFestivalMapVOList.add(goodsFestivalMapVO);
					}
					if(goodsFestivalMapVOList.size() > 0) {
						IOPResultDto<IObjectVO> dtoGoodsFestivalMap = iGoodsFestivalMapService.addGoodsFestivalMaps(goodsFestivalMapVOList);
						if(!dtoGoodsFestivalMap.isSuccess()) {
							return ERROR;
						}
					}
				}
				//商品赠送对象关联
				if(targetIds.length >= 1) {
					TargetVO targetVO = null;
					GoodsTargetMapVO goodsTargetMapVO = null;
					List<GoodsTargetMapVO> goodsTargetMapVOList = new ArrayList<GoodsTargetMapVO>();
					for(int i=0;i<targetIds.length;i++) {
						targetVO = new TargetVO();
						targetVO.setTargetId(targetIds[i]);
						goodsTargetMapVO = new GoodsTargetMapVO();
						goodsTargetMapVO.setGoodsVO(goodsVOTrans);
						goodsTargetMapVO.setMapId(0);
						goodsTargetMapVO.setTargetVO(targetVO);
						goodsTargetMapVOList.add(goodsTargetMapVO);
					}
					if(goodsTargetMapVOList.size() > 0) {
						IOPResultDto<IObjectVO> dtoGoodsTargetMap = iGoodsTargetMapService.addGoodsTargetMaps(goodsTargetMapVOList);
						if(!dtoGoodsTargetMap.isSuccess()) {
							return ERROR;
						}
					}
				}
				return SUCCESS;
			}else {
				return ERROR;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}
	}

}
